package info

import (
	"encoding/json"
	"strconv"
	"time"

	"github.com/yusufpapurcu/wmi"
	"gopkg.in/ini.v1"
)

type Win32_Bus struct {
	Availability                uint16 `json:"可用性"`
	BusNum                      uint32 `json:"总线逻辑编号"`
	BusType                     uint32 `json:"物理总线类型"`
	Caption                     string `json:"说明"`
	ConfigManagerErrorCode      uint32 `json:"配置错误代码"`
	ConfigManagerUserConfig     bool   `json:"用户定义配置"`
	CreationClassName           string
	Description                 string    `json:"描述"`
	DeviceID                    string    `json:"总线ID"`
	ErrorCleared                bool      `json:"报告错误"`
	ErrorDescription            string    `json:"措施的信息"`
	InstallDate                 time.Time `json:"安装时间"`
	LastErrorCode               uint32    `json:"最后错误代码"`
	Name                        string    `json:"名称"`
	PNPDeviceID                 string    `json:"插即用逻辑设备标识符"`
	PowerManagementCapabilities []uint16  `json:"电源相关功能数组"`
	PowerManagementSupported    bool      `json:"电源管理"`
	Status                      string    `json:"状态"`
	StatusInfo                  uint16    `json:"逻辑设备状态"`
	SystemCreationClassName     string
	SystemName                  string `json:"系统名称"`
}

func BusInfo() (info []Win32_Bus, err error) {
	err1 := wmi.Query("Select * from Win32_Bus", &info)
	if err1 != nil {
		return nil, err1
	}

	return info, nil
}

func JsonBusInfo() (f []byte, err error) {
	cpu, err := BusInfo()
	if err != nil {
		return f, err
	}
	t, jsonerr := json.Marshal(cpu)
	if jsonerr != nil {
		return f, jsonerr
	}
	return t, nil

}
func MapBusInfo() (data []map[string]interface{}, err error) {

	bus, err := JsonBusInfo()
	if err != nil {
		return data, err
	}
	json.Unmarshal(bus, &data)
	cfg, err1 := ini.Load("./info/busconfig.ini")
	if err1 != nil {
		return data, err
	}
	//fmt.Println(cfg.Section(cfg.Section("").Key("理总线类型").String()).Key("1").String())
	list := []string{"物理总线类型", "逻辑设备状态", "配置错误代码"}
	for _, v := range data {
		for _, vl := range list {
			if a, ok := v[vl].(float64); ok {
				//fmt.Println(a)
				v[vl] = cfg.Section(cfg.Section("").Key(vl).String()).Key(strconv.Itoa(int(a))).String()
			} else {
				continue
			}

		}
	}
	return data, nil
}
